package com.z.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.z.common.context.UserContext;
import com.z.common.web.vo.request.PageVo;
import com.z.sys.entity.Dept;
import com.z.sys.entity.Role;
import com.z.sys.mapper.RoleMapper;
import com.z.sys.service.IDeptService;
import com.z.sys.service.IRoleService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
/**
 * 角色管理Service业务层处理
 *
 * @author z
 * @date 2022-11-07
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper,Role> implements IRoleService {

    @Resource
    private IDeptService deptService;

    /**
     * 查询角色管理
     * @param role 角色管理
     * @param pageVo
     * @return 角色管理 分页集合
     * */
    @Override
    public PageInfo<Role> selectRolePage(Role role, PageVo pageVo) {
        PageHelper.startPage(pageVo.getPage(), pageVo.getLimit());
        List<Role> data = baseMapper.selectRoleList(role);
        return new PageInfo<>(data);
    }

    @Override
    public List<Role> selectRoleByDeptId(Long deptId) {
        // 当前人员权限级别
        int powerLevel = UserContext.getPowerLevel("人事");

        String nameLike = "无权限_";
        if(deptId == 1 && powerLevel == 0){
            nameLike = "集团_";
        } else {
            Dept dept = deptService.getOne(new QueryWrapper<Dept>().select("id,code").eq("id", deptId));
            if(dept.getCode().split("-").length > 2 && powerLevel < 4){
                nameLike = "班组_";
            } else if (dept.getCode().split("-").length == 2 && powerLevel < 3) {
                nameLike = "科室_";
            } else if (dept.getCode().split("-").length == 1 && powerLevel < 2) {
                nameLike = "单位_";
            }

        }
        return baseMapper.selectList(new QueryWrapper<Role>().select("id,name"));
    }

    @Override
    public List<Role> selectUnitAdminByDeptId(Long deptId) {
        // 当前人员权限级别
        int powerLevel = UserContext.getPowerLevel("人事");

        String namePer = "无权限_";
        if(deptId == 1 && powerLevel == 0){
            namePer = "集团_";
        } else {
            Dept dept = deptService.getOne(new QueryWrapper<Dept>().select("id,code").eq("id", deptId));
            if(dept.getCode().split("-").length > 2 && powerLevel < 4){
                namePer = "班组_";
            } else if (dept.getCode().split("-").length == 2 && powerLevel < 3) {
                namePer = "科室_";
            } else if (dept.getCode().split("-").length == 1 && powerLevel < 2) {
                namePer = "单位_";
            }

        }
        return baseMapper.selectList(new QueryWrapper<Role>().select("id,name").eq("name", namePer+"人事管理员"));
    }

}
